#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <error.h>

#define N 128
#define IP "10.0.8.6"
#define PORT 8888

int main()
{
    struct sockaddr_in myaddr;
    int sockfd;
    int iRet;
    socklen_t addrlen = sizeof(myaddr);
    char buf[N] = "";
    sockfd =  socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd == -1)
    {
        perror("socket()\n");
        exit(1);
    }
    printf("socket fd is %d\n",sockfd);
    myaddr.sin_addr.s_addr = inet_addr(IP);
    myaddr.sin_family = AF_INET;
    myaddr.sin_port = htons(PORT);
    memset(myaddr.sin_zero, 0, 8);


    iRet = bind(sockfd, (struct sockaddr*)&myaddr, sizeof(myaddr));
    if(iRet == -1)
    {
        perror("bind()");
        exit(1);
    }
    
   while(1)
   {

    iRet = recvfrom(sockfd, buf, N, 0, (struct sockaddr*)&myaddr, &addrlen);
 
    if(iRet == -1)
    {
        perror("recvfrem()");
        exit(1);
    }
        printf("recv buf %s",buf);
   }

    close(sockfd);
    return 0;
}